#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <ctype.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#include <sys/epoll.h>

#include "MEpoll.h"
#include "UdpSocket.h"

#define  MAX_EVENTS     5

int main()
{
    UdpSocket socket1(8750);
    UdpSocket socket2(8751);

    MEpoll mepoll(MAX_EVENTS);
    mepoll.Add(socket1.getFd(), EPOLLIN);
    mepoll.Add(socket2.getFd(), EPOLLIN);

    while(1)
    {
        int len;
        char buf[4096];
        int nfds = mepoll.Wait(-1);
        struct epoll_event *events = mepoll.GetEvents();
        if(nfds == -1)
        {
            printf("epoll_wait error!\n");
            return -5;
        }
        printf("nfds: %d\n",nfds);
        for(int i=0;i<nfds;i++)
        {
            memset(buf, 0, sizeof(buf));
            if(events[i].data.fd == socket1.getFd())
            {
                len = socket1.recv(buf, sizeof(buf));
                printf("socket1 recv %s\r\n", buf);
            }   
            else if(events[i].data.fd == socket2.getFd())
            {
                len = socket2.recv(buf, sizeof(buf));
                printf("socket2 recv %s\r\n", buf);
            }
        }
    }

    return 0;
}
